﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;

namespace LittleBee.Utils
{
    public class AppConfigSeciton
	{
        NameValueCollection? _settings;

        public AppConfigSeciton(string strSecName)
		{
            _settings = ConfigurationManager.GetSection(strSecName) as NameValueCollection;
            //if (_settings == null)
            //    throw new NullReferenceException($"cannot found the specific section: {strSecName}");
        }

        public bool IsSectionExist()
        {
            return !(_settings == null);
        }
        
        public bool IsKeyExist(string key)
        {
            if (IsSectionExist())
            {
                return _settings![key] != null;
            }
            return false;
        }

        public string GetValue(string Key, string strDef)
        {
            string _value = strDef;

            if (IsKeyExist(Key))
            {
                _value = (_settings![Key])!;
            }
            return _value;
        }
    }

/*    class DBConfiguration : ConfigurationSection
    {
        [ConfigurationProperty("type", IsRequired = true)]
        [DefaultSettingValue("MySql")]
        public string Type
        {
            get { return this["type"].ToString(); }
            set { this["type"] = value; }
        }

        [ConfigurationProperty("connectionString", IsRequired = true)]
        public string ConnectionString
        {
            get { return this["connectionString"].ToString(); }
            set { this["connectionString"] = value; }
        }
    }

    public class SiteConfiguration : ConfigurationSection
    {
        [ConfigurationProperty("ServerID", IsRequired = true)]
        [DefaultSettingValue("0")]
        public UInt16 ServerID
        {
            get { return (UInt16)this["ServerID"]; }
            set { this["ServerID"] = value; }
        }

        [ConfigurationProperty("SiteID", IsRequired = true)]
        [DefaultSettingValue("0")]
        public UInt16 SiteID
        {
            get { return (UInt16)this["SiteID"]; }
            set { this["SiteID"] = value; }
        }
    }
*/
    /*
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="SQLConfiguration" type="ConfigurationDemo.SQLConfiguration,ConfigurationDemo"/>
    <section name="AccountConfiguration" type="ConfigurationDemo.AccountConfiguration,ConfigurationDemo"/>
  </configSections>
  <SQLConfiguration type="MSSQL" connectionString="server=.;integrated security=sspi;database=Northwind"></SQLConfiguration>
  <AccountConfiguration>
    <users username="liunian" password="123456"></users>
  </AccountConfiguration>
  <system.net>
    <mailSettings>
      <smtp from="liunian@qq.com">
        <network />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>
    */
    /*
        public class AccountConfiguration : ConfigurationSection
        {
            [ConfigurationProperty("users", IsRequired = true)]
            public AccountSectionElement Users
            {
                get { return (AccountSectionElement)this["users"]; }
            }
        }

        public class AccountSectionElement : ConfigurationElement
        {
            [ConfigurationProperty("username", IsRequired = true)]
            public string UserName
            {
                get { return this["username"].ToString(); }
                set { this["username"] = value; }
            }

            [ConfigurationProperty("password", IsRequired = true)]
            public string Password
            {
                get { return this["password"].ToString(); }
                set { this["password"] = value; }
            }
        }
    */
}
